package leichtenschlag.MundoSoundboard;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;

public class Soundboard extends Activity {
    protected MediaPlayer mp[];

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mundo_soundboard);
        
        //Create mediaPlayers for onclick events
        mp = new MediaPlayer[6];
        mp[0] = MediaPlayer.create(this, R.raw.ahhh);
        mp[1] = MediaPlayer.create(this, R.raw.boner_rage);
        mp[2] = MediaPlayer.create(this, R.raw.dont_die);
        mp[3] = MediaPlayer.create(this, R.raw.gratifying);
        mp[4] = MediaPlayer.create(this, R.raw.hate_you);
        mp[5] = MediaPlayer.create(this, R.raw.clockman);
        Button ahhh = (Button)findViewById(R.id.ahhh);
        Button bonerRage = (Button)findViewById(R.id.bonerRage);
        Button dontDie = (Button)findViewById(R.id.dontDie);
        Button gratifying = (Button)findViewById(R.id.gratifying);
        Button hateYou = (Button)findViewById(R.id.hateYou);
        Button clockman = (Button)findViewById(R.id.clockman);
        
        //set opacity of buttons
        AlphaAnimation anim = new AlphaAnimation(1, 0.3f);
        anim.setDuration (0);
        anim.setFillAfter(true);
        ahhh.startAnimation(anim);
        bonerRage.startAnimation(anim);
        dontDie.startAnimation(anim);
        gratifying.startAnimation(anim);
        hateYou.startAnimation(anim);
        clockman.startAnimation(anim);
        
        if (ahhh != null)	setOnClickEvents(ahhh, 0, true);
        if (bonerRage != null)	setOnClickEvents(bonerRage, 1, true);
        if (dontDie != null)	setOnClickEvents(dontDie, 2, true);
        if (gratifying != null)	setOnClickEvents(gratifying, 3, true);
        if (hateYou != null)	setOnClickEvents(hateYou, 4, false);
        if (clockman != null)	setOnClickEvents(clockman, 5, false);
        
        //Info page
        Button infoButton = (Button) findViewById(R.id.info);
        if (infoButton != null)
        {
        	infoButton.setOnClickListener(new ButtonHandler());
        }
        
    }
    private class ButtonHandler implements View.OnClickListener
    {
    	public void onClick(View v)
    	{
    		handleInfoButtonClick();
    	}	
    }
    
    private void handleInfoButtonClick()
    {
    	startActivity(new Intent(this, Soundboard_info.class));
    }
    
    public void setOnClickEvents(Button button, final int index, final boolean start)
    {
    	button.setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    	    	//if (start)
    	    	//{
    	    		try {
    	    			mp[index].start();
    	    		}
    	    		catch (IllegalArgumentException iae) {
    	    		}
    	    	//}
    	    	//else
    	    	//{
    	    	//	try {
    	    	//		mp[index].pause();
    	    	//	}
    	    	//	catch (IllegalArgumentException iae) {
    	    	//	}
    	    	//}
    		}
    	});
    }
}